﻿using System.Drawing;
using System.Xml.Linq;
using QASPRibbon.Helpers;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Represents a Color control in the SharePoint Server Ribbon.
    /// </summary>
    public class RibbonColor : RibbonElement
    {
        #region Properties

        /// <summary>
        /// The name of a CSS selector to apply to the table cell that represents the color.
        /// </summary>
        public string Style
        {
            get { return _style.Localized(); }
            set { _style = value; }
        }

        /// <summary>
        /// A color string to be passed with the command event when the color is selected.
        /// </summary>
        public string Color
        {
            get { return _color; }
            set { _color = value; }
        }

        /// <summary>
        /// A hexadecimal color code to use for the background color of the table cell, such as "#FF0000" (red).
        /// </summary>
        public string DisplayColor
        {
            get { return _displayColor.Localized(); }
            set { _displayColor = value; }
        }

        #endregion

        #region Internal

        internal override string XmlTag
        {
            get { return Schema.Color; }
        }

        internal override XElement AsXElement()
        {
            if (string.IsNullOrEmpty(DisplayColor))
                DisplayColor = Color;
            else if (string.IsNullOrEmpty(Color))
                Color = DisplayColor;
            var element = new XElement(XmlTag);

            element.Add(new XAttribute(Schema.Title, Title));
            if (Sequence > 0)
                element.Add(new XAttribute(Schema.Sequence, Sequence));
            if (!string.IsNullOrEmpty(Style))
                element.Add(new XAttribute(Schema.Style, Style));
            if (!string.IsNullOrEmpty(Color))
                element.Add(new XAttribute(Schema.Color, Color));
            if (!string.IsNullOrEmpty(DisplayColor))
                element.Add(new XAttribute(Schema.DisplayColor, DisplayColor));
            return element;
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates and initializes a new instance of the RibbonColor.
        /// </summary>
        public RibbonColor() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonColor.
        /// </summary>
        /// <param name="title">Title</param>
        /// <param name="color">Color</param>
        public RibbonColor(string title, string color)
        {
            Title = title;
            Color = color;
            DisplayColor = color;
        }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonColor.
        /// </summary>
        /// <param name="color">Color</param>
        public RibbonColor(Color color)
        {
            Title = color.Name;
            Style = string.Empty;
            DisplayColor = Color = ColorTranslator.ToHtml(color);
        }

        #endregion

        #region Private

        private string _displayColor = string.Empty;
        private string _color = string.Empty;
        private string _style = string.Empty;

        #endregion
    }
}
